<?php

declare(strict_types=1);
namespace plugin\banmichaoqiangbizhi\common;

use think\App;
use think\facade\Db;
use think\facade\Log;
use plugin\banmichaoqiangbizhi\model\PluginBanmichaoqiangbizhiSettings as Settings;
use plugin\banmichaoqiangbizhi\model\PluginBanmichaoqiangbizhiArticlePayOrder as ArticlePayOrder ;
use plugin\banmichaoqiangbizhi\model\PluginBanmichaoqiangbizhiPayOrder as PayOrder ;

use think\facade\Cache;

class Common
{
    protected $plugin ='banmichaoqiangbizhi';     //插件别名
    //获取开发者uid
    public function get_d_uid($uid)
    {
        $developer_id = Db::name('plugin_banmichaoqiangbizhi_plugin_developer')->where('uid', $uid)->value('id');
        return $developer_id;
    }
    //获取并处理php中的配置文件 和用户保存的设置合并
    //   $field从配置数据表获取的字段
    // $files从应用插件获取的配置php文件
    //   $map查询条件
    public function get_php_other($field, $files, $map=[])
    {
        $config           =   get_set_config($this->plugin, $field, $files);
     
        //php文件中原始配置导航头
        $data['th_list']  =    $config['php_config']['th_list'];
         
        //php文件中原始配置文件
        $original         =    $config['php_config']['list'];
          
          
        //用户保存的配置文件
        $list             =    !empty($config['sql_config'])?$config['sql_config']:[];
          
          
        $field_list       =    $config['php_config']['field_list'];
          
          
        //合并配置文件和用户保存的数据并去除不需要合并的字段
        foreach ($original as $key => $value) {
            foreach ($list as $k => $val) {
                if ($value['id']== $val['id']) {
                    //先合并以免有新字段
                    $original[$key] =  array_merge($value, $val);
                    foreach ($field_list as $fkey => $fvalue) {
                        $original[$key][$fvalue] =  $value[$fvalue];
                    }
                }
            }
        }
          
          
        $list = $original;
       
        //如果有条件
        if (count($map)>0) {
            foreach ($map as $key => $value) {
                $map_field =  $key;
                $map_value =  $value;
            }
     
            $list = array_filter($list, function ($student) use ($map_field,$map_value) {
                return $student[$map_field] == $map_value;
            });
        }
        $list = array_values($list);
        $data['list']  =$list;
        return $data;
    }
    
    
    
    //判断是否是会员
    public function is_vip($mid)
    {
        $user_vip = Db::name('plugin_banmichaoqiangbizhi_vip_user')->where('uid', $mid)->find();
        if (!$user_vip) {
            return false;
        }
        $time = time();
        if ($time > $user_vip['end_time']) {
            return false;
        }
        //未过期
        return true;
    }
       
  
    public function plugin_price($price)
    {
        $plugin_sell_percentage = Cache::get('plugin_sell_percentage');
                  
        if (!$plugin_sell_percentage) {
            $get_config=  plugin_config(0);
            $plugin_sell_percentage = isset($get_config['plugin_sell_percentage'])?$get_config['plugin_sell_percentage']:0;
             
            Cache::set('plugin_sell_percentage', $plugin_sell_percentage, 3600);
        }
                  
        $plugin_sell_percentage= $plugin_sell_percentage/100;
        $jia =  $price*$plugin_sell_percentage;
                 
                
        $price =  $price + $jia;
        return $price;
    }
    
    
    //支付成功 写入订单
    public function update_pay($data)
    {
        $openid = '';
        Db::startTrans();
        try {
            $map['OrderNo'] =  $data['OrderNo'];
            $order_find     =  Db::name('plugin_banmichaoqiangbizhi_pay_order')->where($map)->find();
            if (isset($order_find['payment_status']) && $order_find['payment_status'] > 0) {
                //回调已完成无需回调
                Log::write('已回调一次' . $data['OrderNo']);
                return 200;
                die;
            }
            if (!empty($data['openid'])) {
                $openid = $data['openid'];
            }
            $order_find['openid'] = $openid;
            $order_find['transaction_id'] = $data['transaction_id'];
            $order_find['payer_total'] = $data['payer_total'];
            $array = [
            //1已付款
            "payment_status" => 1,
            //openid
            "openid" => $openid,
            //支付平台订单号
            "transaction_id" => $data['transaction_id'],
            //实际支付
            "payer_total" => $data['payer_total'],
            //支付时间
            "update_time" => time() ,
            //回调的服务器
            "transaction_ip" =>  request()->ip() ,
            "order_json" =>      $data['order_json'],
            ];
            //回调成改变状态
            $is_pay_id = (new PayOrder())->updates($map, $array, $field = '');
            if ($is_pay_id) {
                //这里可填写确认支付后的逻辑//
                $this->pay_completed($order_find);
                Db::commit();
                return 200;
            }
        } catch (\Exception $ex) {
            Log::write($ex);
            Db::rollback();
            return 0;
        }
    }
    //支付完成
    //处理自己的业务逻辑
    //消息通知
    public function pay_completed($order_find)
    {
        // Log::write('支付完成处理逻辑');
        
        if ($order_find['types'] == 'plugin') {
            $this->plugin_order($order_find);
        } elseif ($order_find['types'] == 'vip') {
            $this->vip_order($order_find);
        } elseif ($order_find['types'] == 'saas') {
            $this->saas_order($order_find);
        } elseif ($order_find['types'] == 'vip_team') {
            $this->vip_team_order($order_find);
        } elseif ($order_find['types'] == 'saas_team') {
            $this->saas_team_order($order_find);
        } elseif ($order_find['types'] == 'plugin_team') {
            $this->team_plugin_order($order_find);
        }
    }
     
     
    //应用拼团成功
    public function team_plugin_order($order_find)
    {
        $order_team_find   =   Db::name('plugin_banmichaoqiangbizhi_plugin_order_team')->where('id', $order_find['team_id'])->find();
        $uid = $order_find['uid'];
        

        //添加参与团的用户
        Db::name('plugin_banmichaoqiangbizhi_plugin_order_team_participate')->insert([
                      'uid'            =>            $uid ,
                      'order_team_id'  =>            $order_team_find['id']  ,
                      'pay_order_id'   =>            $order_find['id'] ,
                      'create_time'    =>            time() ,
         ]);
         
        $team_list =  Db::name('plugin_banmichaoqiangbizhi_plugin_order_team_participate')
                     ->where('order_team_id', $order_team_find['id'])
                     ->select()
                     ->toArray();
       
    
        if (count($team_list)  >=  $order_team_find['team_num']) {
        
            //设置成已成团
            Db::name('plugin_banmichaoqiangbizhi_plugin_order_team_participate')->where('order_team_id', $order_team_find['id'])->update(['is_ok_tuan' => 1 ,'team_time' =>time()]);
            Db::name('plugin_banmichaoqiangbizhi_plugin_order_team')
            ->where('id', $order_find['team_id'])
            ->update(['is_ok_tuan' => 1 ,'team_time' =>time()]);
            //已拼成处理会员订单
            $text='购买应用分成';
            foreach ($team_list as $key => $val) {
                $order_find =  Db::name('plugin_banmichaoqiangbizhi_pay_order')->where('id', $val['pay_order_id'])->find();
                $this->is_plugin_ok($text, $order_find);
            }
        }
    }
     
    //SAAS拼团已购买支付成功
    public function saas_team_order($order_find)
    {
        $order_team_find   =   Db::name('plugin_banmichaoqiangbizhi_saas_order_team')->where('order_no', $order_find['team_order_no'])->find();
        $uid = $order_find['uid'];
        

        //添加参与团的用户
        Db::name('plugin_banmichaoqiangbizhi_saas_order_team_participate')->insert([
                      'uid'            =>            $uid ,
                      'order_team_id'  =>            $order_team_find['id']  ,
                      'pay_order_id'   =>            $order_find['id'] ,
                      'create_time'    =>            time() ,
         ]);
         
        $team_list =  Db::name('plugin_banmichaoqiangbizhi_saas_order_team_participate')->where('order_team_id', $order_team_find['id'])->select()->toArray();
       
    
        if (count($team_list)  >=  $order_team_find['team_num']) {
        
            //设置成已成团
            Db::name('plugin_banmichaoqiangbizhi_saas_order_team_participate')->where('order_team_id', $order_team_find['id'])->update(['is_ok_tuan' => 1 ,'team_time' =>time()]);
            Db::name('plugin_banmichaoqiangbizhi_saas_order_team')->where('order_no', $order_find['team_order_no'])->update(['is_ok_tuan' => 1 ,'team_time' =>time()]);
            //已拼成处理会员订单
            $text='拼团购买了SAAS版授权分成';
            foreach ($team_list as $key => $val) {
                $order_find =  Db::name('plugin_banmichaoqiangbizhi_pay_order')->where('id', $val['pay_order_id'])->find();
                $this->is_saas_ok($text, $order_find);
            }
        }
    }
    
    //拼团成功  助力成功 都购买成功
    public function is_saas_ok($text, $order_find)
    {
        $uid  = $order_find['uid'];
        $url  = $order_find['value'];
        $pay_data = [
           'uid'                =>     $order_find['uid'],
           'url'                =>     $url,
           'auth_code'          =>     '',
           'create_time'        =>     time() ,
           'userip'             =>     ip() ,
         ];
        Db::name('plugin_banmichaoqiangbizhi_saas_url')->insertGetId($pay_data);
        //分销分成
        $this->is_distribution_money('saas_proportion', $uid, $order_find['money'], $order_find['id'], $text);
         
        return 200;
    }
    
    //saas单独购买
    public function saas_order($order_find)
    {
        $uid  = $order_find['uid'];
        $url  = $order_find['value'];
        $find         =   Db::name('plugin_banmichaoqiangbizhi_saas_url')->where('url', $url)->find();
        if ($find) {
            return 200;
        }

        if ($order_find['is_team'] == 0) { //不是拼团 则直接购买成功
            $text='单独购买了SAAS版授权分成';
          
            $this->is_saas_ok($text, $order_find);
            return 200;
        }
        if ($order_find['is_team'] == 2) { //助力购买
 
            $team_id =   Db::name('plugin_banmichaoqiangbizhi_saas_order_assistance')->insert([
                   'uid'           =>            $uid ,
                   'url'           =>             $url,
                   'create_time'   =>            time() ,
                   'end_time'      =>            time() + (7 * 86400),
                   'team_num'      =>            5  ,
                   'pay_order_id'      =>        $order_find['id']  ,
                   'order_no'      =>            make_password(10),//随机生成外部单号
            ]);
 
 
            return 200;
        }
        
        

        //拼团购买
        $day      =  7;
        $team_num =  2;
        $team_id =   Db::name('plugin_banmichaoqiangbizhi_saas_order_team')->insertGetId([
                      'uid'           =>            $uid ,
                      'create_time'   =>            time() ,
                      'end_time'      =>            time() + ($day * 86400),
                      'team_num'      =>            $team_num  ,
                      'order_no'      =>            make_password(10),//随机生成外部单号
                      'pay_order_id'  =>            $order_find['id']  ,
               ]);
              
        Db::name('plugin_banmichaoqiangbizhi_saas_order_team_participate')->insert([
                      'uid'            =>           $uid ,
                      'order_team_id'  =>           $team_id  ,
                      'pay_order_id'   =>           $order_find['id'] ,
                      'create_time'   =>            time() ,
               ]);
        return 200;
    }
    //分销 $type订单类型  $uid 购买者uid   $money 购买金额  $text 操作原因
    public function is_distribution_money($type, $uid, $money, $pay_order_id, $text)
    {
        $ret       =   $this->get_php_other('distribution_config', 'set_distribution_config');
       
        foreach ($ret['list'] as  $key => $val) {
            $d_find[$type.$val['id']]  =  $val[$type]/100;
            $d_find['open'.$val['id']] =  $val['open'];
        }
        //一级获得的金额
        $oen_money =  $money * $d_find[$type.'1'];
        //二级获得的金额
        $two_money =  $money * $d_find[$type.'2'];
        //获取师傅id 一级和二级
        $master_find = 	  get_master($uid);
      
      
        //如果有一级师傅 则分成
        if ($master_find['master_oen'] >0) {
            $data['state']       =  1;
            $data['relation_id'] =  $pay_order_id;
            $data['uid']         =  $master_find['master_oen'];
            $data['text']        =  '一级徒弟'.$text;
            $data['money']       =  $oen_money ;
            
            $this->alter_distribution_money($data);
        }
        //如果有二级师傅 则分成
        if ($master_find['master_two'] >0) {
            $data['state']       =  1;
            $data['relation_id'] =  $pay_order_id;
            $data['uid']         =  $master_find['master_two'];
            $data['text']        =  '二级徒弟'.$text;
            $data['money']       =  $two_money ;
            $this->alter_distribution_money($data);
        }
    }
     
    //变更分销者的余额
    public function alter_distribution_money($data)
    {
        $distribution_money    =      Db::name('plugin_banmichaoqiangbizhi_plugin_distribution_money')->lock(true)->where('uid', $data['uid'])->find();
    
        $money_log_data['uid']          =         $data['uid'];
        $money_log_data['number']       =         $data['money'];
        $money_log_data['state']        =         $data['state']; // 1 加  0减
         $money_log_data['relation_id']  =         $data['relation_id'];  //关联表id
         $money_log_data['type']         =         0;
        $money_log_data['text']         =         $data['text'];        //操作说明
              
        $this->add_distribution_money_log($money_log_data);
        if ($data['state']==1) { //新增 金额
            Db::name('plugin_banmichaoqiangbizhi_plugin_distribution_money')->where('uid', $data['uid'])->inc('money', $data['money'])->inc('total_money', $data['money'])->update();
        } else {  //减少余额
            Db::name('plugin_banmichaoqiangbizhi_plugin_distribution_money')->where('uid', $data['uid'])->dec('money', $data['money'])->update();
        }
    }
 
    /**
    * @notes  增加分销余额日志
    * @$data
    * @author
    * @date 2024/01/12 21:46
    */
    public function add_distribution_money_log($data)
    {
        $number            =         $data['number'];
     
        //当前余额
        $statistics_number =         $data['number'];
       
        
        $map['uid']        =         $data['uid'];
        $find              =    Db::name('plugin_banmichaoqiangbizhi_plugin_distribution_money_log')->where($map)->order('id', 'desc')->find();
        if ($find) {
            if ($data['state'] == 0) {
                $statistics_number =   $find['statistics_number'] - $number;
            } else {
                $statistics_number =   $find['statistics_number'] + $number;
            }
        }
   
         
         
        $data['statistics_number'] =    $statistics_number;
        $data['ip']                =    request()->ip();
        $data['time']              =    time();
        
        return  Db::name('plugin_banmichaoqiangbizhi_plugin_distribution_money_log')->insertGetId($data);
    }
    
    //vip拼团已购买支付成功
    public function vip_team_order($order_find)
    {
        $order_team_find   =   Db::name('plugin_banmichaoqiangbizhi_vip_order_team')->where('order_no', $order_find['team_order_no'])->find();
        $uid = $order_find['uid'];
        
        $vip_find   =   Db::name('plugin_banmichaoqiangbizhi_vip')->where('id', $order_team_find['vip_id'])->find();
          
     
        //添加参与团的用户
        Db::name('plugin_banmichaoqiangbizhi_vip_order_team_participate')->insert([
                      'uid'            =>            $uid ,
                      'order_team_id'  =>            $order_team_find['id']  ,
                      'pay_order_id'   =>            $order_find['id'] ,
                      'create_time'    =>            time() ,
         ]);
         
        $team_list =  Db::name('plugin_banmichaoqiangbizhi_vip_order_team_participate')->where('order_team_id', $order_team_find['id'])->select()->toArray();
          
        if (count($team_list)  >=  $order_team_find['team_num']) {
            //设置成已成团
            Db::name('plugin_banmichaoqiangbizhi_vip_order_team_participate')
            ->where('order_team_id', $order_team_find['id'])
            ->update(['is_ok_tuan' => 1 ,'team_time' =>time()]);
            
            Db::name('plugin_banmichaoqiangbizhi_vip_order_team')
            ->where('order_no', $order_find['team_order_no'])
            ->update(['is_ok_tuan' => 1 ,'team_time' =>time()]);
            
            //已拼成处理会员订单
            $text='拼团购买了VIP分成';
            foreach ($team_list as $key => $val) {
                $order_find =  Db::name('plugin_banmichaoqiangbizhi_pay_order')->where('id', $val['pay_order_id'])->find();
                
                $this->is_vip_ok($text, $order_find, $vip_find);
            }
        }
    }
    
    public function is_vip_ok($text, $order_find, $vip_find)
    {
        $uid = $order_find['uid'];
        $find = Db::name('plugin_banmichaoqiangbizhi_vip_user')->where('uid', $uid)->find();
        $end_time =time() + (365 *86400);
        if ($find) {//续费
        
            if ($find['end_time']>time()) {
                $end_time = $find['end_time']  + (365 *86400);
            }
        
            Db::name('plugin_banmichaoqiangbizhi_vip_user')->where('uid', $uid)->update([
                            'create_time' =>    time() ,
                            'end_time'    =>    $end_time ,
                            'pay_order'   =>    $vip_find['id'] ,
                            'money'       =>    $order_find['money']
                            ]);
        } else {
            //新增会员
            Db::name('plugin_banmichaoqiangbizhi_vip_user')->insert([
                                           'create_time' => time() ,
                                           'end_time'    => $end_time  ,
                                           'uid'         => $uid ,
                                           'pay_order'   => $vip_find['id'] ,
                                           'money'       => $order_find['money']
                                           ]);
        }
                   
        //分销分成
        $this->is_distribution_money('vip_proportion', $uid, $order_find['money'], $order_find['id'], $text);
        return 200;
    }
    
    
    //VIP助力成功
    public function is_vip_assistance($order_find)
    {
        $text='vip助力完成';
        $vip_find =   Db::name('plugin_banmichaoqiangbizhi_vip')->where('id', $order_find['value'])->find();
        $this->is_vip_ok($text, $order_find, $vip_find);
    }
     
    //vip单独已购买成功
    public function vip_order($order_find)
    {
        $vip_find =   Db::name('plugin_banmichaoqiangbizhi_vip')->where('id', $order_find['value'])->find();
        $uid = $order_find['uid'];
        if ($vip_find['is_pin']==0) { //单独购买
            $text='单独购买了VIP分成';
            $this->is_vip_ok($text, $order_find, $vip_find);
            return 200;
        }
        if ($vip_find['is_pin']==2) { //助力购买
          
            $team_id =   Db::name('plugin_banmichaoqiangbizhi_vip_order_assistance')->insert([
                      'uid'           =>            $uid ,
                      'vip_id'        =>            $vip_find['id']  ,
                      'create_time'   =>            time() ,
                      'end_time'      =>            time() + ($vip_find['pin_day'] * 86400),
                      'team_num'      =>            $vip_find['team_num']  ,
                      'pay_order_id'      =>        $order_find['id']  ,
                      'order_no'      =>            make_password(10),//随机生成外部单号
               ]);
              
            return 200;
        }
         
        //拼团购买
        $team_id =   Db::name('plugin_banmichaoqiangbizhi_vip_order_team')->insertGetId([
                      'uid'           =>            $uid ,
                      'vip_id'        =>            $vip_find['id']  ,
                      'pay_order_id'      =>        $order_find['id']  ,
                      'create_time'   =>            time() ,
                      'end_time'      =>            time() + ($vip_find['pin_day'] * 86400),
                      'team_num'      =>            $vip_find['team_num']  ,
                      'order_no'      =>            make_password(10),//随机生成外部单号
               ]);
              
        Db::name('plugin_banmichaoqiangbizhi_vip_order_team_participate')->insert([
                      'uid'            =>           $uid ,
                      'order_team_id'  =>           $team_id  ,
                      'pay_order_id'   =>           $order_find['id'] ,
                      'create_time'   =>            time() ,
               ]);
        return 200;
    }
    
    
    
    //应用购买成功
    public function is_plugin_ok($text, $order_find)
    {
        $find= Db::name('plugin_banmichaoqiangbizhi_plugin_order')->where('id', $order_find['value'])->find();
        Db::name('plugin_banmichaoqiangbizhi_plugin_order') ->where('id', $order_find['value']) ->update([
                  'update_time' =>  time() ,
                  'payment_status' => 1,
                 ]);
        $map['uid']        =    $order_find['uid'];
        $map['plugin_id']  =    $find['value'];
        if (!Db::name('plugin_banmichaoqiangbizhi_user_buy_plugin')->where($map)->find()) {
            $data = [
                         'create_time' => time(),
                         'uid' => $order_find['uid'] ,
                         'plugin_id' => $find['value']
                        ];
            Db::name('plugin_banmichaoqiangbizhi_user_buy_plugin')->insert($data);
        }
        
        //开发者加余额
        // $this->plugin_developer_ok($plugin_order_find);
        
        if ($order_find['money']>10) {
            //分销分成
            $this->is_distribution_money('proportion', $order_find['uid'], $order_find['money'], $order_find['id'], $text);
        }
    }
    
    //开发者加余额
    public function plugin_developer_ok($buy_plugin_find)
    {
        if ($buy_plugin_find['money']==0) {
            $money=0;
        } else {
            $money    =   $buy_plugin_find['money'] / (1 + 0.3);
        }
        
        //开发者uid
        
        $uid  = Db::name('plugin_banmichaoqiangbizhi_plugin')->where('id', $buy_plugin_find['value'])->value('uid');
        
        $data['state']       =  1;// 1 加  0减
        $data['relation_id'] =  $buy_plugin_find['id'];
        $data['uid']         =  $uid;
        $data['text']        =  $buy_plugin_find['text'];
        $data['money']       =  $money ;
        $this->alter_developer_money($data);
    }
 
    //变更的余额
    public function alter_developer_money($data)
    {
        $distribution_money    =      Db::name('plugin_banmichaoqiangbizhi_plugin_developer_money')->lock(true)->where('uid', $data['uid'])->find();
    
        $money_log_data['uid']          =         $data['uid'];
        $money_log_data['number']       =         $data['money'];
        $money_log_data['state']        =         $data['state']; // 1 加  0减
        $money_log_data['relation_id']  =         $data['relation_id'];  //关联表id
        $money_log_data['type']         =         0;
        $money_log_data['text']         =         $data['text'];        //操作说明
              
        $this->add_developer_money_log($money_log_data);
        if ($data['state']==1) { //新增 金额
            Db::name('plugin_banmichaoqiangbizhi_plugin_developer_money')->where('uid', $data['uid'])->inc('money', $data['money'])->inc('total_money', $data['money'])->update();
        } else {  //减少余额
            Db::name('plugin_banmichaoqiangbizhi_plugin_developer_money')->where('uid', $data['uid'])->dec('money', $data['money'])->update();
        }
    }
    
    /**
    * @notes  增加开发者余额日志
    * @$data
    * @author
    * @date 2024/01/12 21:46
    */
    public function add_developer_money_log($data)
    {
        $number            =         $data['number'];
        //当前余额
        $statistics_number =         $data['number'];
        $map['uid']        =         $data['uid'];
        $find              =    Db::name('plugin_banmichaoqiangbizhi_plugin_developer_money_log')->where($map)->order('id', 'desc')->find();
        if ($find) {
            if ($data['state'] == 0) {
                $statistics_number =   $find['statistics_number'] - $number;
            } else {
                $statistics_number =   $find['statistics_number'] + $number;
            }
        }
        $data['statistics_number'] =    $statistics_number;
        $data['ip']                =    request()->ip();
        $data['time']              =    time();
        return  Db::name('plugin_banmichaoqiangbizhi_plugin_developer_money_log')->insertGetId($data);
    }
    
    
    
    
    //插件订单已购买成功
    public function plugin_order($order_find)
    {
        $uid =  $order_find['uid'];
        if ($order_find['is_team']==0) { //用户单独购买
            $text='单独购买应用分成';
            $this->is_plugin_ok($text, $order_find);
            return 200;
        }
        
        if ($order_find['is_team']==2) { //用户助力购买
            $team_id =   Db::name('plugin_banmichaoqiangbizhi_plugin_order_assistance')->insert([
                   'uid'           =>            $uid ,
                   'create_time'   =>            time() ,
                   'end_time'      =>            time() + (7 * 86400),
                   'team_num'      =>            5  ,
                   'pay_order_id'      =>        $order_find['id']  ,
                   'order_no'      =>            make_password(10),//随机生成外部单号
                   'plugin_id'     =>            $order_find['plugin_id']
            ]);
            return 200;
        }
       
        if ($order_find['is_team']==1) { //用户拼团购买
            //拼团购买
            $day      =  7;
            $team_num =  2;
            $team_id =   Db::name('plugin_banmichaoqiangbizhi_plugin_order_team')->insertGetId([
                      'uid'           =>            $uid ,
                      'plugin_id'     =>            $order_find['plugin_id'],
                      'create_time'   =>            time() ,
                      'end_time'      =>            time() + ($day * 86400),
                      'team_num'      =>            $team_num  ,
                      'order_no'      =>            make_password(10),//随机生成外部单号
                      'pay_order_id'  =>            $order_find['id']  ,
               ]);
              
            Db::name('plugin_banmichaoqiangbizhi_plugin_order_team_participate')->insert([
                      'uid'            =>           $uid ,
                      'order_team_id'  =>           $team_id  ,
                      'pay_order_id'   =>           $order_find['id'] ,
                      'create_time'   =>            time() ,
               ]);
            return 200;
        }
    }
    
    //创建支付订单
    public function article_add_pay($data)
    {
        $array = [
            "text" =>$data['text'],             //支付说明
            "money" =>$data['money'],          //支付金额
            "value"=>$data['id'],              //主题id
            "types" =>  $data['types'],        //支付标识
            "create_time"=>time(),
            "userip"=>request()->ip()
         ];
        $order_id =(new ArticlePayOrder())->adds($array, $field='');
        $data['order_id']=$order_id;
        //添加到收银台统一支付
        return $this->pay_order($data);
    }
    
    //创建收银台 统一支付回调管理
    public function pay_order($data)
    {
        $array = [
            "uid"          =>    $data['uid'],                     //订单创建者uid
            "OrderNo"      =>    $this->IsOrderNo(),              //订单号
            "order_name"   =>    $data['text'],                   //订单名称
            "value"        =>    $data['order_id'],               //关联自己的订单表id
            "types"        =>    $data['types'],                 //自定义标识 可用于回调
            "money"        =>    $data['money'],                  //实付金额
            "openid"       =>    $data['openid'],                 //下单者openid 可为空 用于退款
            "create_time"  =>    time(),
            "update_time"  =>    time(),
            "userip"       =>    request()->ip()
         ];
        $id =(new PayOrder())->adds($array, $field='');
        $array['id']=$id;
        return $array;
    }
    /** 生成订单信息公共方法
     * @param $type
     * @return string
     */
    public function IsOrderNo()
    {
        //订单号主体
        $order_id_main = date('YmdHis') . rand(10000000, 99999999);
   
        return $order_id_main;
    }
    
    //获取基本设置
    public function settings($output)
    {
        $config =   plugin_config();
        foreach ($output as $key => $value) {
            $find[$value]=$config[$value];
        }
        return $find;
    }
    
    //保存key设置
    //$output  string  需要保存的key
    //$request    前端传的参数
    public function settings_save($output, $request)
    {
        $config =   plugin_config();
          
       
        foreach ($output as $key => $value) {
            if (isset($request[$value])) {
                $config[$value]= $request[$value];
            }
        }
              
              
        
        return $config;
    }
      
      
    public function is_set_validates($type, $string)
    {
        if ($type=='json') {
            return $this->is_json($string);
        }
           
        return true;
    }
    //判断是否是json格式
    public function is_json($string)
    {
        json_decode($string);
        return json_last_error() === JSON_ERROR_NONE;
    }
    
    
     
    //订单商品售后操作日志
    public function goods_order_log($data)
    {
        $data_log = [
            'goods_order_id' => $data['goods_order_id'],//订单id
            'admin_id' => $data['admin_id'],//管理员id
            'type' => $data['type'],
            'name' => $data['name'],//操作说明
            'refund_image' =>$data['refund_image'],
            'uid' =>$data['uid'],
            'status' => 2,
            'create_time' => time(),
           ];
        Db::name('plugin_banmichaoqiangbizhi_goods_user_cart_order_goods_log')->insertGetId($data_log);
    }
     
    public function is_wx_pay_()
    {
        $set_find = Db::name('plugin_banmichaoqiangbizhi_settings')->where('key', 'wx_set')->find();
        return json_decode($set_find['value'], true);
    }
     
     
     
     
    //微信小程序消息推送
    public function wx_mch_wechat_sendMaster()
    {
        $set_find = Db::name('plugin_banmichaoqiangbizhi_set')->where('id', 1)->find();
        $post_data = [
                'touser' => 'ogmS35NezxeKK2vYVSf5oqR1RjrI',
                'template_id' => 'uJDgqsFEDw2sO6-snNOBb-UPlZ4xIla7lewQ9bmP9UE',
                'page' => '/pages/index/index',//你点击消息要跳转的页面
                'miniprogram_state' => "developer",//跳转小程序类型：developer为开发版；trial为体验版；formal为正式版；默认为正式版
                'lang' => 'zh_CN',
                'data'=>[
                    'thing1'=>[
                        "value"=> "你可以睡觉了"
                    ],
                    'thing2'=>[
                        "value"=> "你已经很多天没有睡了"
                    ],
                    'date4'=>[
                        "value"=> "2023-04-06 07:30:40"
                    ],
                    'name3'=>[
                        "value"=> "帅气的黑大王"
                    ],
                    'thing5'=>[
                        "value"=> "帅气的黑大王"
                    ]
 
                ]
     ];
        $post_data = json_encode($post_data);
        $appId = $set_find['wx_appid'];
        $appSecret = $set_find['wx_mch_key'];
        $a=    banmi_wx_mch_msg($appId, $appSecret, $post_data);
    }
    
    
    
    
    
    //抖音小程序 支付 配置
    ///$pay_code = mp   mp_pay
    public function data_douyin_pay($pay_code)
    {
        $root =root_path().'/bmapp/';
        $set_find=   plugin_config();
        if (empty($set_find['dou_appid'])) {
            ajaxmsg('请配置抖音小程序的app_id ', 0);
        }
        if (empty($set_find['dou_secret'])) {
            ajaxmsg('请配置抖音小程序的secret ', 0);
        }
        if (empty($set_find['dou_merchant_id'])) {
            ajaxmsg('请配置抖音小程序商户号 ', 0);
        }
        if (empty($set_find['dou_salt'])) {
            ajaxmsg('请配置抖音小程序商户号salt', 0);
        }
        if (empty($set_find['dou_token'])) {
            ajaxmsg('请配置抖音小程序商户号token', 0);
        }
        
        
        $pay_data=[
       //同步回调
      'app_id'         => $set_find['dou_appid'],
      'secret'        => $set_find['dou_secret'],
      'merchant_id'   => $set_find['dou_merchant_id'],
      'salt'          => $set_find['dou_salt'],
      'token'         => $set_find['dou_token'],
    
    ];
          
        return $pay_data;
    }
    
    //支付宝支付配置
    //$pay_code='app';  mp
    public function data_alipay_pay($pay_code)
    {
        $root      =   root_path().'/bmapp/';
        $set_find  =   plugin_config(1);
        
        
        
        //支付宝小程序支付配置 都是证书模式
        if ($pay_code=='mp'&& empty($set_find['mp_ali_appid'])) {
            ajaxmsg('请配置支付宝小程序的app_id ', 0);
        }
        if ($pay_code=='mp'&&empty($set_find['mp_ali_rsaprivatekey'])) {
            ajaxmsg('请配置支付宝小程序应用私钥 ', 0);
        }
        if ($pay_code=='mp'&&empty($set_find['mp_ali_alipayrsaPublicKey'])) {
            ajaxmsg('请配置支付宝小程序应用公钥证书 ', 0);
        }
        if ($pay_code=='mp'&&empty($set_find['mp_ali_public_key'])) {
            ajaxmsg('请配置小程序支付宝公钥证书 ', 0);
        }
        if ($pay_code=='mp'&&empty($set_find['mp_ali_rootcert'])) {
            ajaxmsg('请配置小程序支付宝根证书 ', 0);
        }
        
        //app或h5支付
        if ($pay_code=='app'&& empty($set_find['ali_pay_appid'])) {
            ajaxmsg('请配置支付宝分配的app_id ', 0);
        }
        if ($pay_code=='app'&&empty($set_find['ali_private_key'])) {
            ajaxmsg('请配置支付宝应用私钥 ', 0);
        }
        if ($pay_code=='app'&&empty($set_find['ali_app_cert_public_key'])) {
            ajaxmsg('请配置支付宝应用公钥证书 ', 0);
        }
        if ($pay_code=='app'&&empty($set_find['ali_public_key'])) {
            ajaxmsg('请配置支付宝公钥证书 ', 0);
        }
        if ($pay_code=='app'&&empty($set_find['alipay_root_cert'])) {
            ajaxmsg('请配置支付宝根证书 ', 0);
        }
        
        
        
        $pay_data=[
       //同步回调
       // 必填-支付宝分配的 app_id
      'ali_pay_appid'   => $pay_code=='app'? $set_find['ali_pay_appid']:$set_find['mp_ali_appid'],
       // 必填-应用私钥 字符串或路径
      'ali_private_key'   => $pay_code=='app'?$set_find['ali_private_key']:$set_find['mp_ali_rsaprivatekey'],
       // 必填-应用公钥证书
      'ali_app_cert_public_key'   => $pay_code=='app'?$root. $set_find['ali_app_cert_public_key']:$root.$set_find['mp_ali_alipayrsaPublicKey'],
       // 必填-支付宝公钥证书
      'ali_public_key'   => $pay_code=='app'?$root.$set_find['ali_public_key']:$root.$set_find['mp_ali_public_key'],
      // 必填-支付宝根证书 路径
      'alipay_root_cert'   => $pay_code=='app'? $root.$set_find['alipay_root_cert']:$root.$set_find['mp_ali_rootcert'],
      // 选填-服务商模式下的服务商 id，
   
      'alipay_service_provider_id' => isset($set_find['alipay_service_provider_id'])?$set_find['alipay_service_provider_id'] : '',
        // 选填-第三方应用授权token
      'alipay_app_auth_token' => isset($set_find['alipay_app_auth_token'])?$set_find['alipay_app_auth_token'] : '',
    
    ];
          
        return $pay_data;
    }
 
  
    //微信小程序支付配置
    public function data_mp_weixin_pay($pay_code)
    {
        $set_find=   plugin_config();
        
        if (empty($set_find['wx_mch_apiclient_key'])) {
            ajaxmsg('请配置微信商户apiclient_key', 0);
        }
        if (empty($set_find['wx_mch_apiclient_cert'])) {
            ajaxmsg('请配置微信商户apiclient_cert', 0);
        }
        if (empty($set_find['wx_mch_id'])) {
            ajaxmsg('请配置你微信商户id', 0);
        }
        if (empty($set_find['wx_mch_key'])) {
            ajaxmsg('请配置你微信商户v3秘钥', 0);
        }
        
        if ($pay_code=='wx_pay_app' && empty($set_find['app_wx_appid'])) {
            ajaxmsg('请配置appid', 0);
        }
        
        if ($pay_code=='mp_weixin_pay' && empty($set_find['wx_appid'])) {
            ajaxmsg('请配置appid', 0);
        }
        if ($pay_code=='mp_weixin_pay' && empty($set_find['wx_secret'])) {
            ajaxmsg('请配置secret', 0);
        }
        if ($pay_code=='h5' && empty($set_find['h5_wx_appid'])) {
            ajaxmsg('请配置支付h5绑定的appid', 0);
        }
   
        if ($pay_code=='请配置QQ小程序appid' && empty($set_find['h5_wx_appid'])) {
            ajaxmsg('请配置支付h5绑定的appid', 0);
        }
        if ($pay_code=='qq_h5'   && empty($set_find['qq_appid'])) {
            ajaxmsg('请配置QQ小程序appid', 0);
        }
        if ($pay_code=='qq_h5'   && empty($set_find['qq_secret'])) {
            ajaxmsg('请配置QQ小程序secret', 0);
        }
        if ($pay_code=='qq_h5'   && empty($set_find['qq_name'])) {
            ajaxmsg('请配置QQ小程序名称', 0);
        }
        //微信商户id v3
        $pay_data['mch_secret_cert']       =          root_path().'bmapp'.$set_find['wx_mch_apiclient_key'];
        $pay_data['mch_public_cert_path']  =          root_path().'bmapp'.$set_find['wx_mch_apiclient_cert'];
        $pay_data['mch_id']                =          $set_find['wx_mch_id'];
        $pay_data['mch_secret_key']        =          $set_find['wx_mch_key'];
        
        //微信小程序
        if (!empty($set_find['wx_appid'])) {
            $pay_data['mini_app_id']                =           $set_find['wx_appid'];
            $pay_data['mini_wx_appid']              =           $set_find['wx_appid'];
            $pay_data['mini_wx_secret']             =           $set_find['wx_secret'];
        }
 
 
        
        //微信支付app
        if (!empty($set_find['app_wx_appid'])) {
            $pay_data['wx_appid']              =          $set_find['app_wx_appid'];
        }
        if (!empty($set_find['h5_wx_appid'])) {
            $pay_data['h5_wx_appid']              =          $set_find['h5_wx_appid'];
        }
        //qq小程序h5支付
        if (!empty($set_find['qq_appid'])) {
            $pay_data['qq_appid']              =          $set_find['qq_appid'];
        }
        if (!empty($set_find['qq_secret'])) {
            $pay_data['qq_secret']              =          $set_find['qq_secret'];
        }
        if (!empty($set_find['qq_name'])) {
            $pay_data['qq_name']                =          $set_find['qq_name'];
        }
        return $pay_data;
    }
}
